<?php

namespace app\admin\model;

use think\Model;

class SystemModel extends Model
{
    protected $table = 'systems';

    protected $hidden = [];

    protected $autoWriteTimestamp = true;

    /**
     * 根据配置组获取配置信息
     * @param array $groups 配置组
     * @return array
     */
    public static function getConfigInfo(array $groups)
    {
        $data = SystemModel::whereIn('group', $groups)->select();
        foreach ($data as $row) {
            $configs[$row['name']] = $row['value'];
        }
        return isset($configs) ? $configs : [];
    }

    /**
     * 配置保存
     * @param array $request 请求信息
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function saveToAll(array $request)
    {
        foreach ($request as $k => $v) {
            SystemModel::update(['value' => $v], ['name' => $k]);
        }
        return baseResponse(0, '保存配置成功');
    }
}
